package com.dream.controller;

import com.dream.pojo.VisitInformation;
import com.dream.service.VisitInformationService;
import com.dream.service.impl.VisitInformationServiceImpl;
import com.dream.utils.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/VisitInformationController.do")
public class VisitInformationController extends BaseServlet{

    VisitInformationService visitInformationService = new VisitInformationServiceImpl();

    /**
     * 分页查询
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void list(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String stuName = request.getParameter("stuName"); //条件1
        String dormitoryId = request.getParameter("dormitoryId"); //条件2 下拉框
        if(dormitoryId==null||"".equals(dormitoryId)){
            dormitoryId="-1";//请选择
        }
        String pageIndex = request.getParameter("pageIndex"); //条件3  当前页码
        if(pageIndex==null||"".equals(pageIndex)){
            pageIndex="1";//默认值
        }
        String pageSize = request.getParameter("pageSize"); //条件5   页面大小
        if(pageSize==null||"".equals(pageSize)){
            pageSize="5";//默认值
        }
        //条数
        long totalCount = visitInformationService.totalCount(stuName, Long.valueOf(dormitoryId));
        //每页数据
        List<VisitInformation> visitInformationList = visitInformationService.list(stuName,Long.valueOf(dormitoryId), Long.valueOf(pageIndex), Long.valueOf(pageSize));
        //封装一个分页的工具类
        PageUtil<VisitInformation> pageUtil=new PageUtil<VisitInformation>(Long.valueOf(pageIndex), Long.valueOf(pageSize),totalCount,visitInformationList);
        //存值转发
        request.setAttribute("pageUtil",pageUtil);
        request.setAttribute("stuName",stuName); //回显搜素条件
        request.setAttribute("dormitoryId",dormitoryId); //回显搜素条件
        request.getRequestDispatcher("viewVisitRecordInfo.jsp").forward(request,response);
    }
}
